package com.good.msgsys.service.impl;

import com.good.msgsys.dao.AwardDao;
import com.good.msgsys.dao.PaperDao;
import com.good.msgsys.entity.Award;
import com.good.msgsys.entity.Paper;
import com.good.msgsys.service.PaperService;
import com.good.msgsys.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author junlong
 * @date 2020/12/31 11:08
 */
@Service
public class PaperServiceImpl implements PaperService {
    @Autowired
    private PaperDao paperDao;
    @Autowired
    private AwardDao awardDao;
    @Autowired
    private IdWorker idWorker;

    @Override
    @Transactional
    public void add(Paper paper) {
        // 论文
        String id = idWorker.nextId() + "";
        paper.setId(id);
        paper.setState(0); // 新增论文时默认状态为0：未提交
        // 成果
        String awardId = idWorker.nextId() + "";
        Award award = new Award();
        award.setId(awardId);
        award.setPaperId(paper.getId());
        // 保存
        paperDao.save(paper);
        awardDao.save(award);
    }

    @Override
    public void deleteById(String id) {
        paperDao.deleteById(id);
    }

    @Override
    public void update(Paper paper) {
        Paper temp = paperDao.findById(paper.getId()).get();
        temp.setName(paper.getName());
        paperDao.save(temp);
    }

    @Override
    public Paper findById(String id) {
        return paperDao.findById(id).get();
    }

    @Override
    public List<Paper> findAll() {
        return paperDao.findAll();
    }
}
